<?php
class My_GraphCalculations {

    function  __construct() {
        Zend_Loader::loadClass('My_Link');
        Zend_Loader::loadClass('My_Wertex');
    }

    /**
     * return all graph links for wertex id
     *
     * @param int $wertexId wertex_id in graph
     * @return <type>
     */
    function getGraphLinks($wertexId, $readByEnd = true){
        $link = new My_Link();
        if($readByEnd){
            $link->readByEnd($wertexId);
        }else{
            $link->readByStart($wertexId);
        }

        $open = array_merge($link->getData());
        $closed = array();
        while(count($open)){
            $closed[] = $open[0];

            $link = new My_Link($open[0]);

            $tml_link = new My_Link();
            $tml_link->readByStart($link->getStart());
            $open = array_merge($open, $tml_link->getData());

            $tml_link = new My_Link();
            $tml_link->readByStart($link->getEnd());
            $open = array_merge($open, $tml_link->getData());

            $tml_link = new My_Link();
            $tml_link->readByEnd($link->getStart());
            $open = array_merge($open, $tml_link->getData());

            $tml_link = new My_Link();
            $tml_link->readByEnd($link->getEnd());
            $open = array_merge($open, $tml_link->getData());

            $open = $this->tidyUpOpen($open, $closed);
        }
        $this->links = $closed;
        return $closed;
    }

    function tidyUpOpen($open, $exclude){
        $clean = array();
        for($i=0;$i<count($open);$i++){
            $add = true;
            for($j=0;$j<count($exclude);$j++){
                if (
                    $open[$i]['start'] == $exclude[$j]['start'] &&
                    $open[$i]['end'] == $exclude[$j]['end']
                ) {
                    $add = false;
                }
            }
            if($add){
                $clean[] = $open[$i];
                $exclude[] = $open[$i];
            }
        }
        return $clean;
    }

    function getElementList($links){
        foreach($links AS $link){
            $list[$link['start']] = null;
            $list[$link['end']] = null;
        }
        $this->elementList = array_keys($list);
        return $this->elementList;
    }

    function getMatrix($wertexId){
        $this->matrix = array();
        $links = $this->getGraphLinks($wertexId, false);

        $list = $this->getElementList($links);
        foreach($list AS $i){
            foreach($list AS $j){
                $this->matrix[$i][$j] = (int)$this->linkExists($i, $j);
            }
        }
        return $this->matrix;
    }

    function linkExists($start, $end){
        if(!is_array($this->links)){
            return false;
        }
        foreach($this->links AS $link){
            if($link['start'] == $start && $link['end'] == $end){
                return true;
            }
        }
        return false;
    }

    function printMatrix($matrix){
        $out .= '<table name="matrix" border="1">';
        
        $out .= '<tr>';
        $out .= '<th>00</th>';
        foreach($matrix AS $i => $arr){
            $out .= '<th>'.$i.'</th>';
        }
        $out .= '</tr>';
        
        foreach($matrix AS $i => $arr){
            $out .= '<tr>';
            $out .= '<th>'.$i.'</th>';
            foreach($arr AS $j => $val){
                $out .= '<td align="center">'.$matrix[$i][$j].'</td>';
            }
            $out .= '</tr>';
        }

        $out .= '</table>';
        return $out;
    }

    function getGraphArray($matrix){
        $elementCount = count($matrix);
        $open = array_keys($matrix);
        $closed = array();

        $out = array();
        foreach($open AS $key){
            foreach($matrix[$key] AS $i => $el){
                if($el == 1){
                    $out[$key][$i] = null;
                }
            }
        }

        foreach($out AS $key => $val){
            foreach($val AS $m => $n){
                if($m == $key){
                    $tmp = $out[$m];
                    unset($out[$key]);
                    $m = $tmp;
                }
            }
        }
        echo '<pre />';
        print_r($out);
    }

}
?>
